package com.slf.main;

import com.slf.common.LoggerUtil;
import com.slf.common.PropsUtil;
import com.slf.common.RunJobHandler;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
 * @Author：时吕发
 * @Package：com.slf
 * @Project：jydatafillctp
 * @name：BatchTasksJob
 * @Date：2025/9/13 16:38
 * @Filename：BatchTasksJob
 * @desc:
 */
public class BatchTasksJob {
    private final static Logger logger = LoggerUtil.getLogger("jydatafillctp");
    public static void main(String[] args) {
        int num = 0;
        String startDate = args[0];
        String endDate = args[1];
        if(startDate == null || startDate.isEmpty()||endDate == null || endDate.isEmpty()){
            logger.severe("任务执行日期不能为空: startDate="+startDate+", endDate="+endDate);
            return;
        }
        String jyDataFilePath = PropsUtil.getProperty("jyDataFilePath");
        String ctpHqFilePath = PropsUtil.getProperty("ctpHqFilePath");
        String exportFilePath = PropsUtil.getProperty("exportFilePath");
        try {
            logger.info("ctpHqFilePath: " + ctpHqFilePath);
            logger.info("jyDataFilePath: " + jyDataFilePath);
            logger.info("exportFilePath: " + exportFilePath);
            logger.info("执行批量任务，开始日期：" + startDate + "，结束日期：" + endDate);

            File file = new File(jyDataFilePath);
            if (!file.exists()||!file.isDirectory()) {
                logger.severe("jyDataFilePath不存在或不是目录：" + jyDataFilePath);
                return;
            }

            File[] files = file.listFiles();
            List<File> fileList = new ArrayList<>();
            for (File f : files) {
                //过滤出csv文件和sifhq_min开头的文件
                String fileName = f.getName();
                if (f.isFile() && f.getName().endsWith(".csv") && f.getName().startsWith("sifhq_min")) {
                    fileList.add(f);
                }
            }
            logger.info("需要执行批量任务完成，共需执行" + fileList.size() + "次任务");
            //按照文件名排序
            fileList.sort((f1, f2) -> f1.getName().compareTo(f2.getName()));

            for (File dayFile : fileList) {
                String fileName = dayFile.getName();
                String runDate = fileName.substring(fileName.lastIndexOf("_")+1, fileName.indexOf("."));
                if(runDate!=null && runDate.compareTo(startDate)>=0 && runDate.compareTo(endDate)<=0){
                    RunJobHandler.runJob(jyDataFilePath, ctpHqFilePath, exportFilePath, runDate);
                    num++;
                }
            }
            logger.info("执行批量任务完成，共执行" + num + "次任务");
        } catch (Exception e) {
            logger.severe("执行任务失败：" + e.getMessage());
            e.printStackTrace();
        }

    }
}
